package com.example.test.wrong;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.test.R;
import com.example.test.all_Myhelper.exam_Myhelper;
import com.example.test.all_Myhelper.test_Myhelper;
import com.example.test.all_Myhelper.wrong_Myhelper;
import com.example.test.exam.Main;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class wrong_list extends AppCompatActivity implements View.OnClickListener {

    public TextView tishi,wrong_timu,wrong_number;
    public TextView wrong_A,wrong_B,wrong_C,wrong_D;
    public Button wrong_up,wrong_down,back;
    public exam_Myhelper exam_myhelper;
    public test_Myhelper test_myhelper;
    public wrong_Myhelper wrong_myhelper;
    public List<String> title_list;
    public List<String> A;
    public List<String> B;
    public List<String> C;
    public List<String> D;
    public List<String> daan;
    public int page=0;

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wrong);
        exam_myhelper = new exam_Myhelper(this);
        test_myhelper = new test_Myhelper(this);
        wrong_myhelper = new wrong_Myhelper(this);
        init();
        wrong_all();
        chance();
    }

    public void init(){
        tishi = (TextView) findViewById(R.id.tishi);
        wrong_timu = (TextView) findViewById(R.id.wrong_timu);
        wrong_number = (TextView) findViewById(R.id.wrong_number);
        wrong_A = (TextView) findViewById(R.id.wrong_A);
        wrong_B = (TextView) findViewById(R.id.wrong_B);
        wrong_C = (TextView) findViewById(R.id.wrong_C);
        wrong_D = (TextView) findViewById(R.id.wrong_D);
        wrong_up = (Button) findViewById(R.id.wrong_up);
        wrong_down = (Button) findViewById(R.id.wrong_down);
        wrong_up.setOnClickListener(this);
        wrong_down.setOnClickListener(this);
        back = (Button) findViewById(R.id.wrong_back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(wrong_list.this, Main.class);
                startActivity(intent);
            }
        });
    }

    public List<String> tool(List<String> abc,int postion){//返回题目title列表
        List<String> list = new ArrayList<>(abc);
        List<String> s = new ArrayList<>();
        SQLiteDatabase db;
        db = exam_myhelper.getReadableDatabase();
        for(int i=0;i<list.size();i++){
            Cursor cursor = db.rawQuery("select * from tiku where _id=?",new String[]{list.get(i)});
            if(cursor.moveToFirst()){
                s.add(cursor.getString(postion));
            }
            cursor.close();
        }
        db.close();
        return s;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void wrong_all(){
        Bundle bundle = getIntent().getExtras();
        List<Integer> wrong_id_list = bundle.getIntegerArrayList("wrong_id");
        List<String> Str = wrong_id_list.stream().map(String::valueOf).collect(Collectors.toList());//integer转string
        title_list = new ArrayList<>(tool(Str,1));
        A = new ArrayList<>(tool(Str,2));
        B = new ArrayList<>(tool(Str,3));
        C = new ArrayList<>(tool(Str,4));
        D = new ArrayList<>(tool(Str,5));
        daan = new ArrayList<>(tool(Str,6));
    }


    public void chance(){
        yemian(page,title_list,A,B,C,D,daan);
    }

    public void yemian(int s,List<String> title,List<String> A,List<String> B,List<String> C,List<String> D,List<String> daan){
        wrong_timu.setText(title.get(s));//题目
        wrong_A.setText("A:   "+A.get(s));//A
        wrong_B.setText("B:   "+B.get(s));//B
        wrong_C.setText("C:   "+C.get(s));//C
        wrong_D.setText("D:   "+D.get(s));//D
        wrong_A.setTextColor(Color.BLACK);
        wrong_B.setTextColor(Color.BLACK);
        wrong_C.setTextColor(Color.BLACK);
        wrong_D.setTextColor(Color.BLACK);
//        wrong_A.isClickable();
        wrong_number.setText((s+1)+"/"+title.size());
        if(daan.get(s).equals("A")){
            wrong_A.setTextColor(Color.RED);
        }else if (daan.get(s).equals("B")){
            wrong_B.setTextColor(Color.RED);
        }else if (daan.get(s).equals("C")){
            wrong_C.setTextColor(Color.RED);
        }else if (daan.get(s).equals("D")){
            wrong_D.setTextColor(Color.RED);
        }


        if(s==0){
            wrong_up.setVisibility(View.GONE);
        }else{
            wrong_up.setVisibility(View.VISIBLE);
            wrong_up.setText("上一题");
        }
        if(s==title.size()-1){
            wrong_down.setText("退出");
        }else{
            wrong_down.setText("下一题");
        }

    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.wrong_up){
            page--;
            yemian(page,title_list,A,B,C,D,daan);
        }else if(v.getId()==R.id.wrong_down && page == title_list.size()-1){
            Intent intent = new Intent(wrong_list.this,Main.class);
            startActivity(intent);
        }else if(v.getId()==R.id.wrong_down){
            page++;
            yemian(page,title_list,A,B,C,D,daan);
        }
    }
}